package system.database.mysql5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javolution.util.FastMap;

import org.apache.log4j.Logger;

import com.aionemu.commons.database.DatabaseFactory;
import com.aionemu.gameserver.dao.InstanceCooldownsDAO;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.PortalCooldown;

public class MySQL5InstanceCooldownsDAO extends InstanceCooldownsDAO
{
	private static final Logger log = Logger.getLogger(MySQL5InstanceCooldownsDAO.class);
	
	public static final String INSERT_QUERY = "INSERT INTO `portal_cooldowns` (`player_id`, `world_id`, `use_delay`, `reuse_time`) VALUES (?,?,?,?)";
	public static final String DELETE_QUERY = "DELETE FROM `portal_cooldowns` WHERE `player_id`=?";
	public static final String SELECT_QUERY = "SELECT `world_id`, `use_delay`, `reuse_time` FROM `portal_cooldowns` WHERE `player_id`=?";

	@Override
	public void loadPortalCooldowns(final Player player)
	{
		Connection con = null;
		FastMap<Integer, PortalCooldown> portalCoolDowns = new FastMap<Integer, PortalCooldown>().shared();
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(SELECT_QUERY);
			
			stmt.setInt(1, player.getObjectId());
			
			ResultSet rset = stmt.executeQuery();
			
			while(rset.next())
			{
				int worldId = rset.getInt("world_id");
				int useDelay = rset.getInt("use_delay");
				long reuseTime = rset.getLong("reuse_time");
									
				if(reuseTime > System.currentTimeMillis())
					portalCoolDowns.put(worldId, new PortalCooldown(reuseTime, useDelay));
			}
			player.setPortalCoolDowns(portalCoolDowns);
			rset.close();
			stmt.close();
		}
		catch(SQLException e)
		{
			log.error(e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	@Override
	public void storePortalCooldowns(final Player player)
	{
		deletePortalCooldowns(player);
		Map<Integer, PortalCooldown> portalCoolDowns = player.getPortalCoolDowns();

		if(portalCoolDowns == null)
			return;

		for(Map.Entry<Integer, PortalCooldown> entry : portalCoolDowns.entrySet())
		{
			final int worldId = entry.getKey();
			final long reuseTime = entry.getValue().getReuseTime();
			final int useDelay = entry.getValue().getUseDelay();

			if(reuseTime < System.currentTimeMillis())
				continue;
			
			Connection con = null;
			
			try
			{
				con = DatabaseFactory.getConnection();
				PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);
						
				stmt.setInt(1, player.getObjectId());
				stmt.setInt(2, worldId);
				stmt.setInt(3, useDelay);
				stmt.setLong(4, reuseTime);
				stmt.execute();
			}
			catch(SQLException e)
			{
				log.error(e);
			}
			finally
			{
				DatabaseFactory.close(con);
			}
		}
	}

	private void deletePortalCooldowns(final Player player)
	{
		Connection con = null;
		
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(DELETE_QUERY);
			
			stmt.setInt(1, player.getObjectId());
			stmt.execute();
		}
		catch(SQLException e)
		{
			log.error(e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	@Override
	public boolean supports(String arg0, int arg1, int arg2)
	{
		return MySQL5DAOUtils.supports(arg0, arg1, arg2);
	}
}